package com.android.chat.connection;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class ServerConnect {
	public static final String SERVER = "http://113.190.240.180:8089";
	public static final int PORT = 5222;
	public static Connection conn = null;
	
	public static Connection getConnection() {
		if (conn == null) {
			ConnectionConfiguration config = new ConnectionConfiguration(SERVER, PORT);
			//config.setCompressionEnabled(true);
			config.setSASLAuthenticationEnabled(false);
			conn = new XMPPConnection(config);
		}
		
		return conn;
	}
	
	public static boolean authenticate(String username, String password) {
        Connection conn = getConnection();     
        try {
        	
        	// Connect to the server
			conn.connect();
			
			//SASLAuthentication.supportSASLMechanism("PLAIN", 0);
			
			// Log into the server     
			conn.login(username, password);
			
			
			
			return conn.isAuthenticated();

		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	public static void logout() {
		conn.disconnect();
		conn = null;
	}
}
